package servlet;

import model.Page;
import model.User;
import service.FavoriteService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 显示收藏列表的Servlet
 */
@WebServlet(name = "favorite_list", urlPatterns = "/favorite_list")
public class FavoriteListServlet extends HttpServlet {
    private FavoriteService favoriteService = new FavoriteService();

    /**
     * 处理显示收藏列表的请求
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 从session中获取用户信息
        User user = (User) request.getSession().getAttribute("user");
        if (user == null) {
            // 用户未登录，跳转到登录页面
            response.sendRedirect("user_login.jsp");
            return;
        }
        
        // 获取当前页码
        int pageNumber = 1;
        String pageStr = request.getParameter("page");
        if (pageStr != null && !pageStr.isEmpty()) {
            pageNumber = Integer.parseInt(pageStr);
        }
        
        // 获取收藏列表
        Page page = favoriteService.getFavoriteGoodsPage(user.getId(), pageNumber);
        
        // 将页面数据保存到request域中
        request.setAttribute("p", page);
        
        // 转发到收藏列表页面
        request.getRequestDispatcher("/favorite_list.jsp").forward(request, response);
    }
} 